package com.example.cdctest.service.impl;

import com.example.cdctest.mapper.AnswersMapper;
import com.example.cdctest.mapper.CategoryMapper;
import com.example.cdctest.mapper.QuestionMapper;
import com.example.cdctest.mapper.QuestionTypeMapper;
import com.example.cdctest.pojo.*;
import com.example.cdctest.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class QuestionServiceImpl implements QuestionService {

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private AnswersMapper answersMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private QuestionTypeMapper questionTypeMapper;

    @Override
    public void insert(Question object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    //通过问题的id,重新编辑的问题标题,重新编辑好的问题解析,提交到数据库修改信息参数
    @Override
    public void update(Question object) {
        questionMapper.updateQuestionById(object);
    }

    @Override
    public Question findById(Integer id) {
        return null;
    }

    @Override
    public List<Question> findAll() {
        return null;
    }

    @Override
    public PageInfo<Question> findPage(int pageSize) {
        return null;
    }

    //添加问题及其选项，问题题目，问题解析，问题正确选项，问题错误选项，问题的类型id,问题类别的id
    @Override
    public void insertQuestion(String subject, String explain, String[] answers, String[] trueAnswers, Integer questionTypeId, Integer categoryId, Admin admin) {
        //先添加问题跟问题解析，问题类型id,问题类别的Id，操作管理员的ID，同时返回问题的主键
        Question question=new Question();
        question.setSubject(subject);
        question.setExplain(explain);

        //创建问题类别的对象,将选中的问题类别的id放进对象中
        Category category=new Category();
        category.setId(categoryId);
        QuestionType questionType=new QuestionType();
        questionType.setId(questionTypeId);

        question.setQuestionType(questionType);
        question.setCategory(category);
        question.setAdmin(admin);

        questionMapper.insertSelective(question);

        int id=question.getId();

        QuestionType questionType1=new QuestionType();
        questionType1.setId(questionTypeId);
        Integer pid=questionTypeMapper.selectTypePid(questionType1);
        //该对应问题类型问题数目加1
        QuestionType questionType2=new QuestionType();
        questionType2.setId(questionTypeId);
        questionType2.setpId(pid);
        addQuestionCount(questionType2);
        //该对应类别问题数目加1
        categoryMapper.updateCategoryQuestionCount(categoryId);
        Question question1=new Question();
        question1.setId(id);
        Answers answers1=null;
        //根据问题的主键，循环添加问题的正确选项
        for (String answer:answers
             ) {
            answers1=new Answers();
            answers1.setQuestion(question1);
            answers1.setAnswerContent(answer);
            answers1.setTrueAnswer(false);
            answersMapper.insertAnswers(answers1);
        }
        //根据问题的id，添加问题的错误选项
        for (String trueAnswer:trueAnswers
             ) {
            answers1=new Answers();
            answers1.setQuestion(question1);
            answers1.setAnswerContent(trueAnswer);
            answers1.setTrueAnswer(true);
            answersMapper.insertAnswers(answers1);
        }
    }

    //递归调用，传值，给该类型数量加1
    @Override
    public void addQuestionCount(QuestionType questionType) {
        //pid 等于1 调用加1的方法
        //pid 不等于1 调用递归
        if(questionType.getpId()==1) {
            questionTypeMapper.updateTypeQuestionCount(questionType);
        }else{
            QuestionType questionType1=new QuestionType();
            Integer pid=questionTypeMapper.selectTypePid(questionType);
            questionType1.setId(questionType.getpId());
            questionType1.setpId(pid);
            addQuestionCount(questionType1);
        }
    }

    @Override
    public List<Question> selectQuestionByCategoryid(Integer id) {
        return questionMapper.selectQuestionByCategoryid(id);
    }

    @Override
    public List<Question> selectQuestionByQuestionTypeid(Integer id) {
        return questionMapper.selectQuestionByQuestionTypeid(id);
    }

    @Override
    public List<Question> selectAllQuestion() {
        return questionMapper.selectAllQuestion();
    }

    //通过页面传的关键字，问题类型，或者页码取值
    @Override
    public PageInfo<Question> findAllQuestions(String subject, int pageNum, int pageSize, Integer questionTypeId) {
        //一页显示多少条
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questions=questionMapper.selectList(subject,questionTypeId);
        //默认导航页码数是8，可以通过参数设置
        PageInfo<Question> pageInfo=new PageInfo<>(questions,10);
        return pageInfo;
    }

    //通过关键字，页码或者问题类型获得查询的数量
    @Override
    public Integer getQuestionCount(String subject, Integer pageNum, Integer pageSize, Integer questionTypeId) {
        //一页显示多少条
        PageHelper.startPage(pageNum,pageNum);
        Integer questionCount=questionMapper.selectQuestionCount(subject,questionTypeId);
        return questionCount;
    }

    //通过当前问题的id,查出这个问题下所有的问题选项
    @Override
    public List<Answers> getAnswersById(Integer id) {
        return answersMapper.selectAllById(id);
    }

    //通过问题选项的id，修改问题的选项内容
    @Override
    public void updateAnswer(Answers answers) {
        answersMapper.updateAnswerById(answers);
    }

    //通过问题的id，删除该id的问题,以及该问题下所有的选项
    @Override
    public void updateQuestionById(Integer id) {
        //通过问题的id，删除该id的问题
        questionMapper.updateQuestionDelById(id);
        //通过问题的id,删除问题下的选项内容
        answersMapper.updateAnswerDelById(id);
    }

    //通过批量问题的id，删除这些id下的问题，及其对应的选项
    @Override
    public void updateBatchQuestion(String ids) {
        //批量删除问题
        questionMapper.updateBatchQuestion(ids);
        //根据问题的id,批量删除问题的选项
        answersMapper.updateBatchAnswers(ids);
    }

    @Override
    public List<Question> randomTest() {
        return questionMapper.randomTest();
    }

}
